package com.cincc.cinccimdemo.common;

import android.content.Context;
import android.util.AttributeSet;

import androidx.constraintlayout.widget.ConstraintHelper;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.constraintlayout.widget.ConstraintSet;
import androidx.transition.TransitionManager;

import com.cincc.cinccimdemo.R;

public class TranslationUtils extends ConstraintHelper {

    private ConstraintSet mResetConstraintSet, mMoreConstraintSet;
    private ConstraintLayout mParent;
    private boolean isMoreLayoutShown;

    public TranslationUtils(Context context, AttributeSet attrs) {
        super(context, attrs);
        mResetConstraintSet = new ConstraintSet();
        mMoreConstraintSet = new ConstraintSet();
        createMoreLayoutConstraint();
        createResetConstraint();
    }

    @Override
    public void updatePostLayout(ConstraintLayout container) {
        super.updatePostLayout(container);
        mParent = container;
    }

    private void createMoreLayoutConstraint() {
        mMoreConstraintSet.clear(R.id.chat_input);
        mMoreConstraintSet.clear(R.id.chat_more);

        mMoreConstraintSet.connect(R.id.chat_input, ConstraintSet.TOP, R.id.swiperefreshlayout, ConstraintSet.BOTTOM);
        mMoreConstraintSet.connect(R.id.chat_input, ConstraintSet.BOTTOM, R.id.chat_more, ConstraintSet.TOP);
        mMoreConstraintSet.centerHorizontally(R.id.chat_input, ConstraintSet.PARENT_ID);
        mMoreConstraintSet.constrainHeight(R.id.chat_input, ConstraintSet.WRAP_CONTENT);
        mMoreConstraintSet.constrainWidth(R.id.chat_input, ConstraintSet.MATCH_CONSTRAINT);

        mMoreConstraintSet.connect(R.id.chat_more, ConstraintSet.TOP, R.id.chat_input, ConstraintSet.BOTTOM);
        mMoreConstraintSet.connect(R.id.chat_more, ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM);
        mMoreConstraintSet.centerHorizontally(R.id.chat_more, ConstraintSet.PARENT_ID);
        mMoreConstraintSet.constrainHeight(R.id.chat_more, ConstraintSet.WRAP_CONTENT);
        mMoreConstraintSet.constrainWidth(R.id.chat_more, ConstraintSet.MATCH_CONSTRAINT);
    }

    private void createResetConstraint() {
        mResetConstraintSet.clear(R.id.chat_input);
        mResetConstraintSet.clear(R.id.chat_more);

        mResetConstraintSet.connect(R.id.chat_input, ConstraintSet.TOP, R.id.swiperefreshlayout, ConstraintSet.BOTTOM);
        mResetConstraintSet.connect(R.id.chat_input, ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM);
        mResetConstraintSet.centerHorizontally(R.id.chat_input, ConstraintSet.PARENT_ID);
        mResetConstraintSet.constrainHeight(R.id.chat_input, ConstraintSet.WRAP_CONTENT);
        mResetConstraintSet.constrainWidth(R.id.chat_input, ConstraintSet.MATCH_CONSTRAINT);

        mResetConstraintSet.connect(R.id.chat_more, ConstraintSet.TOP, R.id.chat_input, ConstraintSet.BOTTOM);
        mResetConstraintSet.centerHorizontally(R.id.chat_more, ConstraintSet.PARENT_ID);
        mResetConstraintSet.constrainHeight(R.id.chat_more, ConstraintSet.WRAP_CONTENT);
        mResetConstraintSet.constrainWidth(R.id.chat_more, ConstraintSet.MATCH_CONSTRAINT);
    }


    public void showMoreLayout() {
        TransitionManager.beginDelayedTransition(mParent);
        mMoreConstraintSet.applyTo(mParent);
        isMoreLayoutShown = true;
    }


    public void hideBottomLayout() {
        TransitionManager.beginDelayedTransition(mParent);
        mResetConstraintSet.applyTo(mParent);
        isMoreLayoutShown = false;
    }

    /**
     * 底部表情布局或底部更多布局是否显示
     */
    public boolean isButtomLayoutShown() {
        return isMoreLayoutShown;
    }

    /**
     * 底部表情布局或底部更多布局是否显示
     */
    public boolean isBottomLayoutShown() {
        return isMoreLayoutShown;
    }

    /**
     * 底部更多布局是否显示
     */
    public boolean isMoreLayoutShown() {
        return isMoreLayoutShown;
    }
}
